package com.heima.behavior.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.behavior.config.CacheService;
import com.heima.behavior.service.ApLikesBehaviorService;
import com.heima.common.constants.message.HotArticleConstants;
import com.heima.common.constants.user.UserRelationConstants;
import com.heima.common.exception.config.CustException;
import com.heima.model.app.UpdateArticleMess;
import com.heima.model.behavior.dtos.LikesBehaviorDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.threadlocal.AppThreadLocalUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class ApLikesBehaviorServiceImpl implements ApLikesBehaviorService {
    @Autowired
    CacheService cacheService;
    @Autowired
    KafkaTemplate kafkaTemplate;

    /**
     * 点赞
     *
     * @param likesBehaviorDto
     * @return
     */
    @Override
    public ResponseResult like(LikesBehaviorDto likesBehaviorDto) {
        //1.参数校验
        if (likesBehaviorDto == null || likesBehaviorDto.getArticleId() == null || checkParam(likesBehaviorDto)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.用户是否登录
        ApUser apUser = AppThreadLocalUtils.getUser();
        if (apUser == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        //3.点赞行为，保存数据

        Integer apUserId = apUser.getId();
        Long articleId = likesBehaviorDto.getArticleId();
        Short operation = likesBehaviorDto.getOperation();
        if (operation== 0) {
            //查询是否已点赞
            Double score = cacheService.zScore(UserRelationConstants.BEHAVIOR_LIKE + apUserId, articleId.toString());
            if (score != null) {
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "已点赞");
            }
            //点赞

            cacheService.zAdd(UserRelationConstants.BEHAVIOR_LIKE + apUserId, articleId.toString(), System.currentTimeMillis());
            log.info("保存点赞数据成功");
            //实时计算文章分值
            UpdateArticleMess mess = new UpdateArticleMess();
            mess.setType(UpdateArticleMess.UpdateArticleType.LIKES);
            mess.setArticleId(likesBehaviorDto.getArticleId());
            kafkaTemplate.send(HotArticleConstants.HOTARTICLE_SCORE_INPUT_TOPIC, JSON.toJSONString(mess));
            log.info("*****kafkaTemplate send like");

        } else {
            //取消点赞

            cacheService.zRemove(UserRelationConstants.BEHAVIOR_LIKE + apUserId, articleId.toString());
            log.info("删除点赞数据成功");
        }

            //4.返回结果
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    private boolean checkParam(LikesBehaviorDto likesBehaviorDto) {
        Short type = likesBehaviorDto.getType();
        Short operation = likesBehaviorDto.getOperation();
        if (type > 2 || type < 0 || operation > 1 || operation < 0) {
            return true;
        }
        return false;
    }

}
